// pages/pick/pick.js
Page({
  data: {
    bottleImg: '/images/empty-bottle.png',
    isPicking: false,
    bottleCount: 100,
    showBottleContent: false,
    currentBottle: null,
    showReplyBox: false,
    replyContent: ''
  },

  onLoad: function () {
    // 初始化页面
  },

  // 捞瓶子
  pickBottle: function () {
    if (this.data.isPicking) return;
    this.setData({
      isPicking: true,
      bottleImg: '/images/empty-bottle.png',
      showBottleContent: false
    });
    // 增加30%概率打捞失败
    if (Math.random() < 0.2) {
      setTimeout(() => {
        wx.showToast({
          title: '这片海域暂时没有漂流瓶哦~',
          icon: 'none',
          duration: 2000
        });
        this.setData({
          bottleImg: '/images/empty-bottle.png',
          isPicking: false
        });
      }, 800);
      return;
    }
    wx.cloud.callFunction({
      name: 'pickBottle',
      data: {},
      success: res => {
        if (res.result && res.result.code === 0) {
          const bottle = res.result.bottle;
          // 通过 openid 查用户信息
          const db = wx.cloud.database();
          db.collection('users').where({ openid: bottle.openid }).get({
            success: userRes => {
              const user = userRes.data[0] || {};
              this.setData({
                bottleImg: '/images/full-bottle.png',
                showBottleContent: true,
                currentBottle: {
                  avatar: user.avatarUrl || '/images/avatar1.png', // 默认头像
                  nickname: user.nickName || '匿名用户',
                  time: this.formatTime(new Date(bottle.time)),
                  type: bottle.typeName,
                  content: bottle.content
                }
              });
            },
            fail: () => {
              this.setData({
                bottleImg: '/images/full-bottle.png',
                showBottleContent: true,
                currentBottle: {
                  avatar: '/images/avatar1.png',
                  nickname: '匿名用户',
                  time: this.formatTime(new Date(bottle.time)),
                  type: bottle.typeName,
                  content: bottle.content
                }
              });
            }
          });
        } else {
          wx.showToast({
            title: res.result.msg || '没有捡到漂流瓶',
            icon: 'none',
            duration: 2000
          });
          this.setData({
            bottleImg: '/images/empty-bottle.png'
          });
        }
        this.setData({ isPicking: false });
      },
      fail: err => {
        wx.showToast({ title: '网络错误', icon: 'none' });
        this.setData({ isPicking: false });
      }
    });
  },

  // 时间格式化
  formatTime: function (date) {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
  },

  // 显示回复框
  showReplyBox: function () {
    this.setData({
      showReplyBox: true
    });
  },

  // 隐藏回复框
  hideReplyBox: function () {
    this.setData({
      showReplyBox: false,
      replyContent: ''
    });
  },

  // 回复内容输入
  onReplyInput: function (e) {
    this.setData({
      replyContent: e.detail.value
    });
  },

  // 发送回复
  sendReply: function () {
    const { replyContent } = this.data;

    if (!replyContent.trim()) {
      wx.showToast({
        title: '回复内容不能为空',
        icon: 'none'
      });
      return;
    }

    // 模拟发送回复
    wx.showLoading({
      title: '发送中...',
      mask: true
    });

    setTimeout(() => {
      wx.hideLoading();

      wx.showToast({
        title: '回复成功！',
        icon: 'success'
      });

      // 隐藏回复框
      this.hideReplyBox();

      // 3秒后自动返回
      setTimeout(() => {
        wx.navigateBack();
      }, 2000);
    }, 1500);
  },

  // 忽略瓶子，继续打捞
  ignoreBottle: function () {
    this.setData({
      showBottleContent: false
    });

    // 延迟后自动开始打捞
    setTimeout(() => {
      this.pickBottle();
    }, 500);
  }
});    